﻿using System;
using System.Collections.Generic;
using System.Text;
using NTL.DAL;

namespace NTL.BLL.Galleries
{
	public class Gallery : BizObject
	{
		private long _id;
		private string _name;
		private DateTime _addedDate;
		private string _addedBy;
		private bool _visible;
		private bool _commentEnabled = false;

		public long Id { get { return _id; } set { _id = value; } }
		public string Name { get { return _name; } set { _name = value; } }
		public DateTime AddedDate { get { return _addedDate; } set { _addedDate = value; } }
		public string AddedBy { get { return _addedBy; } set { _addedBy = value; } }
		public bool Visible { get { return _visible; } set { _visible = value; } }
		public bool CommentEnabled { get { return _commentEnabled; } set { _commentEnabled = value; } }

		public Gallery(long id, string name, DateTime addedDate, string addedBy, bool visible, bool commentEnabled)
		{
			_id = id;
			_name = name;
			_addedDate = addedDate;
			_addedBy = addedBy;
			_visible = visible;
			_commentEnabled = commentEnabled;
		}

		/**********
		 * STATIC *
		 **********/
		public static Gallery FindById(long id)
		{
			Gallery gallery = null;
			string key = "Gallery_"+ id.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				gallery = (Gallery)Cache[key];
			else
			{
				gallery = GetFromDetails(NTLProvider.Gallery.FindById(id));
				CacheData(key, gallery);
			}
			return gallery;
		}
		/*----------*----------*----------*----------*----------*----------*----------*----------*----------*/
		public static List<Gallery> FindByTimeline(int year, long startRowIndex, long maximumRows)
		{
			List<Gallery> galleries = null;
			string key = "NTL_Galleries_" + year.ToString() + "_" + startRowIndex.ToString() + "_" + maximumRows.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				galleries = (List<Gallery>)Cache[key];
			else
			{
				galleries = GetListFromDetailsList(NTLProvider.Gallery.FindByTimeline(year,
					GetPageIndex(startRowIndex, maximumRows), maximumRows));
			}
			return galleries;
		}
		public static List<Gallery> FindByTimeline(int year, int month, long startRowIndex, long maximumRows)
		{
			List<Gallery> galleries = null;
			string key = "NTL_Galleries_" + year.ToString() + "_" + month.ToString() + "_" + startRowIndex.ToString() + "_" + maximumRows.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				galleries = (List<Gallery>)Cache[key];
			else
			{
				galleries = GetListFromDetailsList(NTLProvider.Gallery.FindByTimeline(year, month,
					GetPageIndex(startRowIndex, maximumRows), maximumRows));
			}
			return galleries;
		}
		public static List<Gallery> FindByTimeline(int year, int month, int day, long startRowIndex, long maximumRows)
		{
			List<Gallery> galleries = null;
			string key = "NTL_Galleries_" + year.ToString() + "_" + month.ToString() + "_" + day.ToString() + "_" + startRowIndex.ToString() + "_" + maximumRows.ToString();
			if (Settings.EnableCaching && Cache[key] != null)
				galleries = (List<Gallery>)Cache[key];
			else
			{
				galleries = GetListFromDetailsList(NTLProvider.Gallery.FindByTimeline(year, month, day,
					GetPageIndex(startRowIndex, maximumRows), maximumRows));
			}
			return galleries;
		}
		/*----------*----------*----------*----------*----------*----------*----------*----------*----------*/

		/***********
		 * PRIVATE *
		 ***********/
		private static Gallery GetFromDetails(GalleryDetails record)
		{
			if (record == null) return null;
			return new Gallery(record.Id, record.Name,
				record.AddedDate, record.AddedBy,
				record.Visible, record.CommentEnabled);
		}
		private static List<Gallery> GetListFromDetailsList(List<GalleryDetails> recordset)
		{
			List<Gallery> galleries = new List<Gallery>();
			foreach (GalleryDetails record in recordset)
				galleries.Add(GetFromDetails(record));
			return galleries;
		}
	}
}
